Online-Academy
Look, Read, Understand, Apply

MVC - JSP Servlet

Using Java Beans and Servlets together is the foundation of Java web development. This combination typically implements the MVC (Model-View-Controller) design pattern:

  • Model (Java Bean): Holds the data.
  • Controller (Servlet): Handles the request, processes logic, and populates the Bean.
  • View (JSP): Displays the data stored in the Bean.
Example: Building a simple application where a user enters data, a Servlet processes it, stores it in a Java Bean, and sends it to a JSP for display.

The Java Bean ( The Model): A Java Bean is simply a Java class that follows specific rules: it must have a no-argument constructor, private properties, and public getters/setters. Store this bean in webapps/myapp/WEB-INF/com/example.
Compile that java bean there, such that the class file is also stored there.

package com.example;
import java.io.Serializable;
public class UserBean implements Serializable {
    private String firstName;
    private String email;
    public UserBean() {}
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

The Servlet (The Controller): The Servlet receives the data from the web browser, creates an instance of the Java Bean, populates it, and passes it to the next page.
Store this sevlet in folder: webapps/myapp/WEB-INF/classes/com/example. Compile that servlet such that the class file is also stored in the same folder.

C:\apache-tomcat\webapps\myapp\WEB-INF\classes\com\example\>javac -cp "C:\apache-tomcat\lib\jakarta.servlet-api-6.0.0.jar" UserServlet.java

package com.example;
import com.example.UserBean;
import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {        
        String nameInput = request.getParameter("username");
        String emailInput = request.getParameter("useremail");
        UserBean user = new UserBean();
        user.setFirstName(nameInput);
        user.setEmail(emailInput);
        request.setAttribute("currentUser", user);
request.getRequestDispatcher("profile.jsp").forward(request, response);
    }
}

Way to compile the UserBean and UserServlet, both are stored in the same folder: com/example

PS C:\apache-tomcat-11.0.5\webapps\myapps\WEB-INF\classes>javac -d . -cp "C:\apache-tomcat-11.0.5\lib\jakarta.servlet-api-6.0.0.jar;." com/example/*.java

The HTML Form (The Input): This is the entry point where the user types their information. Note the action matches the Servlet annotation.
Save this HTML form inside webapps\myapp folder.

register.html
    <h2>Registration Form</h2>
    <form action="register" method="post">
        Name: <input type="text" name="username"><br>
        Email: <input type="text" name="useremail"><br>
        <input type="submit" value="Register">
    </form>

The View (JSP) The JSP picks up the Java Bean from the request and displays the data. Save this jsp file inside webapps\myapp folder.

profile.jsp
    <h2>Registration Successful</h2>
    <p>Welcome, <strong>${currentUser.firstName}</strong>!</p>
    <p>We will send updates to: ${currentUser.email}</p>

web.xml (Deployment Descriptor)

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee 
         https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
         version="6.0">

    <servlet>
        <servlet-name>UserServlet</servlet-name>
        <servlet-class>com.example.UserServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/register</url-pattern>
    </servlet-mapping>

</web-app>